package zhoufu.sz.sanxiao_check.presents;

import android.databinding.ObservableArrayList;
import com.google.gson.Gson;
import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.inject.RetrofitQualifier;
import zhoufu.sz.sanxiao_check.models.IDModel;
import zhoufu.sz.sanxiao_check.models.SecureImgEntity;
import zhoufu.sz.sanxiao_check.models.SecureModel;
import zhoufu.sz.sanxiao_check.net.BasicRequestRepository;
import zhoufu.sz.sanxiao_check.net.RequestHelper;
import zhoufu.sz.sanxiao_check.ui.contract.BasicItem4Contract;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/26
 */
public class BasicItem4Presenter implements BasicItem4Contract.Presenter
{

    private BasicRequestRepository mRepository;

    private BasicItem4Contract.View view;

    private SecureModel _model;

    private SecureImgEntity entity;

    @Inject
    @RetrofitQualifier("Observable")
    Gson _gson;

    @Inject
    public BasicItem4Presenter(BasicRequestRepository mRepository)
    {
        this.mRepository = mRepository;
    }

    public void bidnView(BasicItem4Contract.View view)
    {
        this.view = view;
        mRepository.setGson(_gson);
    }

    private void setSize(ObservableArrayList<SecureImgEntity> arrayList, int type)
    {
        for (int i = 0; i < 3; i++)
        {
            if ( arrayList.size() > i )
            {
                arrayList.get(i).setType(type);
            } else
            {
                SecureImgEntity entity = new SecureImgEntity();
                entity.setType(type);
                arrayList.add(i, entity);
            }
        }
    }


    @Override public void onStart()
    {
        mRepository.GetSecure(view.SID(), new RequestHelper.RequestCallBack<SecureModel>()
        {
            @Override public void onPullStart()
            {
                view.pullStart();
            }

            @Override public void onPullFailud(Throwable throwable)
            {
                view.pullFaild();
            }

            @Override public void onPullEnd()
            {
                view.pullEnd();
            }

            @Override public void onPullSuccessed(SecureModel s)
            {
                if ( s.isSuccess() )
                {
                    if ( s.getData().size() > 0 )
                    {
                        _model = s.getData().get(0);
                        setSize(_model.Equipment_Img, 4);
                        setSize(_model.Escape_Img, 3);
                        setSize(_model.Kitchen_Img, 2);
                        setSize(_model.Rests_Img, 6);
                        setSize(_model.Stay_Img, 1);
                        setSize(_model.Switch_Img, 5);
                        view.setModel(_model);
                    }
                } else
                {
                    view.showToast(s.getErrMsg());
                }
            }
        });
    }


    public void showDelDialog(SecureImgEntity entity)
    {
        view.showDelDialog(entity);
    }


    public SecureImgEntity setSecureImgEntity(int type, int index)
    {
        if ( _model == null )
        {
            return null;
        }
        switch (type)
        {
            case 1:
                if ( _model.Stay_Img.size() > index )
                {
                    entity = _model.Stay_Img.get(index);
                }
                break;
            case 2:
                if ( _model.Kitchen_Img.size() > index )
                {
                    entity = _model.Kitchen_Img.get(index);
                }
                break;
            case 3:
                if ( _model.Escape_Img.size() > index )
                {
                    entity = _model.Escape_Img.get(index);
                }
                break;
            case 4:
                if ( _model.Equipment_Img.size() > index )
                {
                    entity = _model.Equipment_Img.get(index);
                }
                break;
            case 5:
                if ( _model.Switch_Img.size() > index )
                {
                    entity = _model.Switch_Img.get(index);
                } else
                {
                    entity = new SecureImgEntity();
                    _model.Switch_Img.add(index, entity);
                }
                break;
            case 6:
                if ( _model.Rests_Img.size() > index )
                {
                    entity = _model.Rests_Img.get(index);
                }
                break;
            default:
                entity = new SecureImgEntity();
                break;
        }
        if ( entity == null )
        {
            return null;
        }
        entity.PhotoLocal.set(entity.PhotoPath.get());
        return entity;
    }

    public SecureImgEntity getEntity()
    {
        return entity;
    }

    @Override public void onPause()
    {

    }

    @Override public void onDestroy()
    {
        mRepository.onDestory();
    }

    @Override public void update(final SecureImgEntity entity)
    {
        mRepository.UpdateSecure(entity, view.SID(),
                new RequestHelper.RequestCallBack<IDModel>()
                {
                    @Override public void onPullStart()
                    {
                        view.pullStart();
                    }

                    @Override public void onPullFailud(Throwable throwable)
                    {
                        view.pullFaild();
                    }

                    @Override public void onPullEnd()
                    {
                        view.pullEnd();
                    }

                    @Override public void onPullSuccessed(IDModel s)
                    {
                        view.pullEnd();
                        if ( s.isSuccess() )
                        {
                            if ( s.getData().size() > 0 )
                            {
                                entity.PhotoID.set(s.getData().get(0).getID());
                            }
                            view.updateSuccese();
                        } else
                        {
                            view.showToast(s.getErrMsg());
                        }
                    }
                });
    }
}
